<template>
  <view class="content" v-if="shopDetail">
    <view class="status_bar"></view>

    <!-- <view class="record">
      <view class="record_price">
        <text>钱包总金额</text>
        <view class="record_price_text">
          <text>￥</text>
          <text>{{ shopDetail.balance }}</text>
        </view>
      </view>
      <view class="record_button">
        <text
          class="income"
          @click="todetail('../Income_details/Income_details')"
          >收支明细</text
        >
        <text @click="todetail('../withdrawal_detail/withdrawal_detail')"
          >提现记录</text
        >
      </view>
    </view> -->

    <view class="main">
      <view class="main_top">
        <image
          src="../../static/jiantous.png"
          mode=""
          class="main_top_img"
          @click="handleBack"
        ></image>

        <view class="main_top_text">现金钱包</view>

        <view class="main_top_right" @click="toTransfer()">转账</view>
      </view>

      <view class="main_price">总金额:</view>
      <!-- <view class="main_num">￥{{ shopDetail.balance }}</view> -->
      <view class="main_num">￥{{ cash_balance }}</view>

      <view class="main_line"></view>

      <view class="main_bottom">
        <!-- <view class="main_item" @click="todetail('../recharge/recharge')">
          <image
            src="../../static/wallet1.png"
            mode=""
            class="main_item_img"
          ></image>

          <view class="main_item_text">充值</view>
        </view> -->

        <view
          class="main_item"
          @click="todetail('../Income_details/Income_details')"
        >
          <image
            src="../../static/wallet2.png"
            mode=""
            class="main_item_img"
          ></image>

          <view class="main_item_text">收支明细</view>
        </view>

        <view
          class="main_item"
          @click="todetail('../withdrawal_detail/withdrawal_detail')"
        >
          <image
            src="../../static/wallet3.png"
            mode=""
            class="main_item_img"
          ></image>

          <view class="main_item_text">提现记录</view>
        </view>
      </view>
    </view>

    <!-- <view class="pricemode" @click="todetail('../recharge/recharge')">
      <text>充值入口</text>
      <image
        src="../../static/jiantou.png"
        mode=""
        class="list_right_img"
      ></image>
    </view> -->

    <view
      class="pricemode"
      v-if="shopDetail && shopDetail.payment_method.length >= 2"
      @click="todetail('../pricemodel/pricemodel')"
    >
      <text>我的收款方式</text>
      <image
        src="../../static/jiantou.png"
        mode=""
        class="list_right_img"
      ></image>
    </view>

    <view class="price">
      <text>提现金额</text>
      <view class="price_input">
        <text>￥</text>
        <input
          v-model="price"
          placeholder="输入提现金额"
          style="flex: 1"
          @input="checkNum"
        />
      </view>
      <text>手续费({{ shopDetail.commission_money }}%)</text>
    </view>

    <view class="listwhole" @click="todetail('../bankcardlist/bankcardlist')">
      <view class="list_new" v-if="!cardstate || !shopDetail.payment_method[0]">
        <image src="/static/add_icon.png" class="list_new_img" />
        <text>添加收款账户</text>
      </view>

      <view class="list_left" v-else>
        <image
          src="../../static/bank_card.png"
          mode=""
          class="list_left_img"
          v-if="
            shopDetail.payment_method[0] &&
            shopDetail.payment_method[0].bank_card_number
          "
        ></image>
        <image
          src="../../static/alipay_background.png"
          mode=""
          class="list_left_img"
          v-if="
            shopDetail.payment_method[0] &&
            shopDetail.payment_method[0].zfb_account
          "
        ></image>

        <view class="list_left_right" v-if="shopDetail.payment_method[0]">
          <text>{{ shopDetail.payment_method[0].real_name }}</text>
          <text style="margin-top: 10rpx">{{
            shopDetail.payment_method[0].bank_card_number
          }}</text>
          <text style="margin-top: 10rpx">{{
            shopDetail.payment_method[0].zfb_account
          }}</text>
        </view>
      </view>

      <view class="list_right_img" v-if="cardstate">
        <image
          src="../../static/jiantou.png"
          mode=""
          class="list_right_img"
        ></image>
      </view>
    </view>

    <view class="button" @click="handlePassOn">确认提现</view>
    <view class="detail">
      <view class="detail_title">提现说明</view>
      <u-parse :html="walllestext" style="color: #ffffff"></u-parse>
      <!-- <view class="detail_title">提现说明</view>
      <view class="detail_row">
1、文字说明文字说明文字说明文字说明文字说明:
1、文字说明文字说明文字说明文字说明文字说明;
1、文字说明文字说明文字说明文字说明文字说明;
1、文字说明文字说明文字说明文字说明文字说明;
    </view> -->
    </view>

    <u-popup
      v-model="Transaction"
      mode="center"
      width="490rpx"
      height="290rpx"
      :border-radius="24"
      :closeable="true"
      :mask-close-able="false"
      @close="jypassword = ''"
    >
      <view class="Transaction_eject">
        <text class="Transaction_top">输入交易密码</text>
        <input
          type="password"
          value=""
          class="Transaction_center"
          v-model="jypassword"
        />
        <text class="Transaction_bottom" @click="password">确定</text>
      </view>
    </u-popup>
  </view>
</template>

<script>
import api from "@/api/api.js";
import tip from "@/utils/tip.js";
export default {
  data() {
    return {
      shopDetail: null,
      price: "",
      Transaction: false,
      jypassword: "",
      cardstate: false,
      walllestext: null,
    };
  },
  onLoad(options) {
    this.getRuletext();
  },
  onShow() {
    this.getuserWallet();
  },
  computed: {
    cash_balance() {
      return (
        Number(this.shopDetail.balance) - Number(this.shopDetail.lock_balance)
      ).toFixed(2);
    },
  },
  methods: {
    toTransfer() {
      uni.navigateTo({
        url: `../wallet/wallet_transfer`,
      });
    },
    handleBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
    async getRuletext() {
      let res = await api.getRule({
        method: "get",
        query: { key: "cash_out_rule" },
        loading: "加载中",
      });
      console.log(res, "res");
      this.walllestext = res.data ? res.data.value : "";
    },
    checkNum(e) {
      e.detail.value = e.target.value.match(/^\d*(\.?\d{0,2})/g)[0];
      this.$nextTick(() => {
        this.price = e.detail.value;
      });
    },
    password() {
      if (this.jypassword == "") {
        tip.defaults("请输入交易密码");
      } else {
        // this.Transaction = false
        this.handlePassOn();
      }
    },
    todetail(url) {
      uni.navigateTo({
        url: url,
      });
    },
    async getuserWallet() {
      let res = await api.userWallet({ method: "get", loading: "加载中" });

      if (res.code === 0) {
        // res.data.payment_method.forEach(el => {
        //   if(el.bank_card_number){
        //     el.bank_card_number = el.bank_card_number.replace(/^(\d{4})\d+(\d{4})$/,"$1 **** **** $2");
        //   }
        //   if(el.zfb_account){
        //     el.zfb_account = el.zfb_account.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
        //   }
        // });
        this.shopDetail = res.data;

        this.shopDetail.commission_money =
          this.shopDetail.commission_money * 100;
        if (!this.shopDetail.payment_method.length == 0) {
          this.cardstate = true;
        }
        uni.setStorageSync("walletid", res.data.id);
        let walletcard = JSON.stringify(res.data.payment_method);
        uni.setStorageSync("walletcard", walletcard);
      } else {
        tip.defaults(res.msg);
      }
    },

    async handlePassOn() {
      if (!this.price) {
        tip.defaults("请输入提现金额");
        return;
      }
      if (parseFloat(0) >= parseFloat(this.price)) {
        tip.defaults("金额必须大于零");
        return;
      }
      if (parseFloat(this.shopDetail.balance) < parseFloat(this.price)) {
        tip.defaults("余额不足");
        return;
      }
      if (this.shopDetail.payment_method.length == 0) {
        tip.defaults("请先绑定收款账户");
        return;
      }
      if (!this.jypassword) {
        this.Transaction = true;
        return;
      }
      let res = await api.withdrawal({
        method: "post",
        query: {
          wallet_id: this.shopDetail.id,
          money: this.price,
          payment_id: this.shopDetail.payment_method[0].id,
          operation_password: this.jypassword,
        },
        loading: "加载中",
      });
      console.log(res, "res");
      if (res.code === 0) {
        this.shopDetail.balance = (
          this.shopDetail.balance - this.price
        ).toFixed(2);
        tip.defaults("提现成功");
        this.price = "";
        this.Transaction = false;
      } else {
        tip.defaults(res.msg);
      }
      this.jypassword = "";
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.status_bar {
  margin: 0 -30rpx;
  height: var(--status-bar-height);
  width: 750rpx;
}
.content {
  font-family: PingFangSC-Regular, PingFang SC;
  padding: 0 30rpx 100rpx 30rpx;
  background: $bg-color1;

  min-height: 100vh;
}
.main {
  position: relative;
  margin: 0 -30rpx;
  padding: 0 30rpx;
  height: 412rpx;
  background-image: url(../../static/wallet_bg.png);
  background-repeat: no-repeat;
  // background-size: 100% 500rpx;
  background-size: 100% 412rpx;
  &_top {
    margin-left: -30rpx;
    position: relative;
    // display: flex;

    width: 750rpx;
    height: 88rpx;
    text-align: center;
    &_img {
      position: absolute;
      left: 30rpx;
      top: 23rpx;
      width: 42rpx;
      height: 42rpx;
    }
    &_text {
      margin: 0 auto;
      // width: 68rpx;
      height: 88rpx;
      font-size: 34rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 88rpx;
    }
    &_right {
      position: absolute;
      top: 24rpx;
      right: 30rpx;
      // width: 112rpx;
      height: 40rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 40rpx;
    }
  }
  &_price {
    margin: 60rpx auto 0;
    // width: 128rpx;
    height: 44rpx;
    font-size: 32rx;
    font-weight: 400;
    color: #ffffff;
    line-height: 44rpx;
    text-align: center;
  }
  &_num {
    margin-top: 14rpx;
    // width: 204rpx;
    height: 86rpx;
    // font-size: 44rpx;
    font-size: 72rpx;
    font-weight: bold;
    color: #ffffff;
    line-height: 86rpx;
    text-align: center;
  }
  &_line {
    margin: 40rpx -30rpx 0 -30rpx;
    width: 750rpx;
    height: 2rpx;
    background: #d8d8d8;
    opacity: 0.18;
  }
  &_bottom {
    // padding: 28rpx 0 0 0;
    margin: 0 -30rpx;
    padding: 24rpx 2rpx 0 2rpx;
    display: flex;
    align-items: center;
  }
  &_item {
    // padding: 0 48rpx;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    // text-align: center;
    &_img {
      width: 32rpx;
      height: 32rpx;
    }
    &_text {
      margin-left: 6rpx;
      // width: 56rpx;
      height: 40rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 40rpx;
    }
  }
  &_item:not(:first-child) {
    border-left: 2rpx solid rgba($color: #ffffff, $alpha: 0.5);
  }
}
.title {
  height: 40rpx;
  font-size: 28rpx;
  font-weight: 400;
  color: #0c1125;
  line-height: 40rpx;
}
.record {
  margin-top: 30rpx;
  display: flex;
  align-items: center;
  width: 690rpx;
  height: 242rpx;
  border-radius: 24rpx;
  background-image: url(../../static/wallet_background.png);
  background-repeat: no-repeat;
  background-size: 100% 242rpx;
  display: flex;
  justify-content: space-between;
  position: relative;
  .record_price {
    display: flex;
    flex-direction: column;
    margin: 30rpx;
    text {
      color: #ffffff;
      font-size: 16px;
      margin: 10rpx 0;
    }
    .record_price_text {
      margin: 10rpx 0;
      text:nth-child(2) {
        color: #ffffff;
        font-size: 24px;
        font-weight: bold;
      }
    }
  }
  .record_button {
    position: absolute;
    bottom: 30rpx;
    right: 15rpx;
    color: #ffffff;
    text {
      margin: 0 15rpx;
      border: 2rpx solid #ffffff;
      border-radius: 32rpx;
      padding: 8rpx 16rpx;
    }
  }
}
.price {
  margin-top: 30rpx;
  width: 100%;
  background: $bg-color3;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  text {
    margin: 30rpx;
    color: $text-color1;
  }
  text:first-child {
    font-size: 16px;
  }
  text:nth-child(2) {
    font-size: 12px;
  }
  .price_input {
    height: 100rpx;
    display: flex;
    align-content: flex-start;
    input {
      height: 120rpx;
      line-height: 120rpx;
      color: $text-color1;
    }
    border-bottom: 2rpx solid #313535;
    text {
      font-size: 22px;
      font-weight: 500;
    }
  }
}
.listwhole {
  display: flex;
  align-content: center;
  justify-content: space-between;
  margin: 30rpx 0;
  padding: 30rpx;
  border-radius: 24rpx;
  background-color: $bg-color3;
  height: 150rpx;
  .list_new {
    color: $text-color1;
    margin: auto;
    .list_new_img {
      width: 24rpx;
      height: 24rpx;
      margin-right: 10rpx;
    }
  }
  .list_left {
    display: flex;
    .list_left_img {
      width: 80rpx;
      height: 80rpx;
    }
    .list_left_right {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 30rpx;
      color: $text-color1;
      font-size: 28rpx;
    }
  }
  .list_right_img {
    margin: auto 0;
    width: 30rpx;
    height: 30rpx;
    // margin-top: 15rpx;
  }
}

.button {
  margin-top: 50rpx;
  width: 690rpx;
  height: 90rpx;
  color: #ffffff;
  background: $button;
  background-repeat: no-repeat;
  // background-attachment: fixed;
  background-size: 100% 100%;
  border-radius: 16rpx;

  font-size: 32rpx;
  font-weight: 400;
  line-height: 90rpx;
  text-align: center;
}
.detail {
  margin-top: 70rpx;
  &_title {
    font-size: 30rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 40rpx;
    margin: 30rpx 0;
  }
  &_row {
    margin-top: 30rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #bbbbbe;
    line-height: 40rpx;
    white-space: break-spaces;
  }
  &_row + &_row {
    margin-top: 20rpx;
  }
}
.input_center {
  color: #ffffff;
  // height: 260rpx;
  // padding: 30rpx 0 0rpx;
  word-break: break-all;
  margin: 30rpx;
  max-height: 300rpx;
}
.Transaction_eject {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  height: 290rpx;
  background-color: $bg-color1;
  .Transaction_top {
    margin-top: 50rpx;
    font-size: 32rpx;
    color: $text-color1;
  }
  .Transaction_center {
    padding: 0 20rpx;
    border: 2rpx solid #262934;
    background: $bg-color3;
    border-radius: 16rpx;
    height: 85rpx;
    width: 80%;
    color: #ffffff;
  }
  .Transaction_bottom {
    font-size: 32rpx;
    color: $text-color1;
    margin-bottom: 30rpx;
  }
}
.pricemode {
  color: $text-color1;
  background-color: $bg-color3;
  display: flex;
  justify-content: space-between;
  padding: 30rpx;
  margin-top: 30rpx;
  image {
    margin: auto 0;
    width: 30rpx;
    height: 30rpx;
  }
  border-radius: 24rpx;
  font-size: 28rpx;
}
</style>